package com.mixed.mixedserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.mixed.mixedserver.common.BaseResponse;
import com.mixed.mixedserver.common.ResultUtils;
import com.mixed.mixedserver.exception.BusinessException;
import com.mixed.mixedserver.model.enums.ErrorCode;
import com.mixed.mixedserver.model.vo.user.UserVO;
import com.mybatisflex.core.paginate.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.mixed.mixedserver.model.entity.User;
import com.mixed.mixedserver.service.UserService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import java.util.List;

/**
 * 用户 控制层。
 *
 * @author abluedog
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册用户。
     *
     * @param user 用户
     * @return {@code true} 保存成功，{@code false} 保存失败
     */
    @PostMapping("register")
    @Operation(description="注册用户")
    public BaseResponse<Boolean> save(@RequestBody @Parameter(description="用户")User user) {
        return ResultUtils.success(userService.register(user));
    }

    @PostMapping("login")
    @Operation(description="用户登录")
    public BaseResponse<User> login(@RequestBody @Parameter(description="用户")User user) {
        User login = userService.login(user);
        if (login == null){
           throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(login);
    }

    @GetMapping("getLoginUser")
    @Operation(description="获取登录用户")
    public BaseResponse<UserVO> getLoginUser(HttpServletRequest request) {
        return ResultUtils.success(userService.getLoginUserVO(request));
    }

    /**
     * 退出登录
     *
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("logout")
    @Operation(description="退出登录")
    public BaseResponse<Boolean> logout(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        StpUtil.logout(loginUser.getId());
        return ResultUtils.success(true);
    }

    /**
     * 根据主键更新用户。
     *
     * @param user 用户
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @Operation(description="根据主键更新用户")
    public BaseResponse<Boolean> update(@RequestBody @Parameter(description="用户主键") User user) {
        return  ResultUtils.success(userService.updateById(user));
    }

    /**
     * 查询所有用户。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @Operation(description="查询所有用户")
    public BaseResponse<List<User>> list() {
        return  ResultUtils.success(userService.list());
    }

}
